package com.sharp.authorization.controller.admin;

import com.sharp.authorization.annotation.auth.AdminAuthRole;
import com.sharp.authorization.annotation.auth.Auth;
import com.sharp.authorization.service.permit.AuthRoleService;
import com.sharp.authorization.service.permit.PermitCoreService;
import com.sharp.authorization.vo.permit.BindRolePermitReq;
import com.sharp.authorization.vo.role.*;
import com.sharp.kernel.config.response.Response;
import com.sharp.kernel.config.response.ResponseHandler;
import com.sharp.kernel.vo.common.CommonResVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * Title: AdminRoleController
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2020/4/9 18:01
 * @description Project Name: Mall
 * @Package: com.srct.service.account.controller.admin
 */
@ApiIgnore
@Auth(AdminAuthRole.class)
@Api(value = "用户角色管理", tags = "后台管理员")
@RestController("AdminRoleController")
@RequestMapping(value = "/admin/role")
public class AdminRoleController {

    @Resource
    private PermitCoreService permitCoreService;
    @Resource
    private AuthRoleService authRoleService;

    @ApiOperation(value = "获取角色列表", notes = "系统管理员准入")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<Response<List<RoleRes>>> queryRole(QueryRoleReq req) {
        List<RoleRes> res = authRoleService.queryRole(req);
        return ResponseHandler.generateResponse(res);
    }

    @ApiOperation(value = "添加角色", notes = "系统管理员准入")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public ResponseEntity<Response<CommonResVO>> addRole(@RequestBody AddRoleReq req) {
        authRoleService.addRole(req);
        return ResponseHandler.commonResponse();
    }

    @ApiOperation(value = "修改角色", notes = "系统管理员准入")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity<Response<CommonResVO>> updateRole(@RequestBody UpdateRoleReq req) {
        authRoleService.updateRole(req);
        return ResponseHandler.commonResponse();
    }

    @ApiOperation(value = "删除角色", notes = "系统管理员准入")
    @RequestMapping(value = "", method = RequestMethod.DELETE)
    public ResponseEntity<Response<CommonResVO>> deleteRole(@RequestBody DeleteRoleReq req) {
        authRoleService.deleteRole(req);
        return ResponseHandler.commonResponse();
    }

    @ApiOperation(value = "获取角色树", notes = "系统管理员准入")
    @RequestMapping(value = "/roleTree", method = RequestMethod.GET)
    public ResponseEntity<Response<List<RoleTreeRes>>> queryRoleTree() {
        List<RoleTreeRes> res = authRoleService.queryRoleTree();
        return ResponseHandler.generateResponse(res);
    }

    @ApiOperation(value = "绑定角色权限", notes = "系统管理员准入")
    @RequestMapping(value = "/permit/bind", method = RequestMethod.POST)
    public ResponseEntity<Response<CommonResVO>> bindUserRole(@RequestBody BindRolePermitReq req) {
        permitCoreService.bindRolePermit(req);
        return ResponseHandler.commonResponse();
    }

}
